package com.getpebble.android.framework.comm;

import android.content.Context;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.common.model.PblDevice;
import com.getpebble.android.framework.DeviceInterfaces.IDeviceMessageReceivedCallback;
import com.getpebble.android.framework.DeviceInterfaces.IDeviceMessageSender;
import com.getpebble.android.framework.endpoint.EndpointRequest;
import com.getpebble.android.framework.endpoint.EndpointSet;
import com.getpebble.android.framework.endpoint.NormalEndpointSet;
import com.getpebble.android.framework.endpoint.PrfEndpointSet;
import com.getpebble.android.framework.endpoint.QueueEndpoint;
import com.getpebble.android.framework.protocol.EndpointId;
import com.google.common.collect.ImmutableSet;
import com.sun.mail.smtp.SMTPMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MessageRouter implements IDeviceMessageReceivedCallback {
    public static final String TAG = MessageRouter.class.getSimpleName();
    private static Map<PblDevice, MessageRouter> sRouterMap = new HashMap();
    private final String ENDPOINT_SET_LOCK = "ENDPOINT_SET_LOCK";
    private EndpointSet mCurrentEndpointSet;
    private PblDevice mDevice;
    private MessageSender mMessageSender;
    private QueueEndpoint mQueueEndpoint;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$EndpointId = new int[EndpointId.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$EndpointId[EndpointId.LOG_DUMP.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$EndpointId[EndpointId.CORE_DUMP.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
        }
    }

    protected MessageRouter(IDeviceMessageSender deviceMessageSender, PblDevice device) {
        this.mDevice = device;
        this.mMessageSender = new MessageSender(deviceMessageSender, device);
        this.mQueueEndpoint = new QueueEndpoint();
    }

    public static MessageRouter getOrCreateRouter(IDeviceMessageSender deviceMessageSender, PblDevice device) {
        if (deviceMessageSender == null) {
            throw new IllegalArgumentException("'deviceMessageSender' cannot be null!");
        } else if (device == null) {
            throw new IllegalArgumentException("'device' cannot be null!");
        } else {
            synchronized ("ROUTER_LOCK") {
                if (sRouterMap.containsKey(device)) {
                    MessageRouter messageRouter = (MessageRouter) sRouterMap.get(device);
                    return messageRouter;
                }
                MessageRouter messageRouter2 = new MessageRouter(deviceMessageSender, device);
                deviceMessageSender.registerBluetoothMessageCallback(messageRouter2);
                sRouterMap.put(device, messageRouter2);
                return messageRouter2;
            }
        }
    }

    public static MessageRouter getRouter(PblDevice device) {
        if (device == null) {
            throw new IllegalArgumentException("'device' cannot be null!");
        }
        MessageRouter messageRouter;
        synchronized ("ROUTER_LOCK") {
            if (sRouterMap.containsKey(device)) {
                messageRouter = (MessageRouter) sRouterMap.get(device);
            } else {
                messageRouter = null;
            }
        }
        return messageRouter;
    }

    public MessageSender getMessageSender(EndpointSet endpointSet) {
        if (endpointSet != null && (endpointSet instanceof EndpointSet)) {
            return this.mMessageSender;
        }
        throw new IllegalArgumentException("Only endpoint sets can get the message sender");
    }

    public PblDevice getDevice() {
        return this.mDevice;
    }

    private EndpointSet getCurrentEndpointSetLocked() {
        EndpointSet endpointSet;
        synchronized ("ENDPOINT_SET_LOCK") {
            endpointSet = this.mCurrentEndpointSet;
        }
        return endpointSet;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public boolean setCurrentEndpointSet(com.getpebble.android.framework.endpoint.EndpointSet r9) {
        /*
        r8 = this;
        r3 = 1;
        r4 = 0;
        r5 = "ENDPOINT_SET_LOCK";
        monitor-enter(r5);
        r6 = r8.mCurrentEndpointSet;	 Catch:{ all -> 0x0103 }
        if (r6 != r9) goto L_0x0012;
    L_0x0009:
        r4 = TAG;	 Catch:{ all -> 0x0103 }
        r6 = "setCurrentEndpointSet: No change";
        com.getpebble.android.common.core.trace.Trace.debug(r4, r6);	 Catch:{ all -> 0x0103 }
        monitor-exit(r5);	 Catch:{ all -> 0x0103 }
    L_0x0011:
        return r3;
    L_0x0012:
        r6 = r8.mCurrentEndpointSet;	 Catch:{ all -> 0x0103 }
        if (r6 != 0) goto L_0x003b;
    L_0x0016:
        r6 = r9 instanceof com.getpebble.android.framework.endpoint.HandshakeEndpointSet;	 Catch:{ all -> 0x0103 }
        if (r6 != 0) goto L_0x003b;
    L_0x001a:
        r3 = TAG;	 Catch:{ all -> 0x0103 }
        r6 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0103 }
        r6.<init>();	 Catch:{ all -> 0x0103 }
        r7 = "Can't move from null endpoint set to non-handshake (";
        r6 = r6.append(r7);	 Catch:{ all -> 0x0103 }
        r6 = r6.append(r9);	 Catch:{ all -> 0x0103 }
        r7 = ")";
        r6 = r6.append(r7);	 Catch:{ all -> 0x0103 }
        r6 = r6.toString();	 Catch:{ all -> 0x0103 }
        com.getpebble.android.common.core.trace.Trace.debug(r3, r6);	 Catch:{ all -> 0x0103 }
        monitor-exit(r5);	 Catch:{ all -> 0x0103 }
        r3 = r4;
        goto L_0x0011;
    L_0x003b:
        r6 = r8.mCurrentEndpointSet;	 Catch:{ all -> 0x0103 }
        r6 = r6 instanceof com.getpebble.android.framework.endpoint.HandshakeEndpointSet;	 Catch:{ all -> 0x0103 }
        if (r6 == 0) goto L_0x0068;
    L_0x0041:
        r6 = r9 instanceof com.getpebble.android.framework.endpoint.NormalEndpointSet;	 Catch:{ all -> 0x0103 }
        if (r6 != 0) goto L_0x0068;
    L_0x0045:
        if (r9 == 0) goto L_0x0068;
    L_0x0047:
        r3 = TAG;	 Catch:{ all -> 0x0103 }
        r6 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0103 }
        r6.<init>();	 Catch:{ all -> 0x0103 }
        r7 = "Can't move from handshake to anything but normal/null (";
        r6 = r6.append(r7);	 Catch:{ all -> 0x0103 }
        r6 = r6.append(r9);	 Catch:{ all -> 0x0103 }
        r7 = ")";
        r6 = r6.append(r7);	 Catch:{ all -> 0x0103 }
        r6 = r6.toString();	 Catch:{ all -> 0x0103 }
        com.getpebble.android.common.core.trace.Trace.debug(r3, r6);	 Catch:{ all -> 0x0103 }
        monitor-exit(r5);	 Catch:{ all -> 0x0103 }
        r3 = r4;
        goto L_0x0011;
    L_0x0068:
        r4 = r8.mCurrentEndpointSet;	 Catch:{ all -> 0x0103 }
        if (r4 == 0) goto L_0x0071;
    L_0x006c:
        r4 = r8.mCurrentEndpointSet;	 Catch:{ all -> 0x0103 }
        r4.destroy();	 Catch:{ all -> 0x0103 }
    L_0x0071:
        r8.mCurrentEndpointSet = r9;	 Catch:{ all -> 0x0103 }
        monitor-exit(r5);	 Catch:{ all -> 0x0103 }
        if (r9 == 0) goto L_0x0011;
    L_0x0076:
        r9.init();
        r4 = r8.mQueueEndpoint;
        r4 = r4.takeMessages();
        r1 = r4.iterator();
    L_0x0083:
        r4 = r1.hasNext();
        if (r4 == 0) goto L_0x0011;
    L_0x0089:
        r2 = r1.next();
        r2 = (com.getpebble.android.bluetooth.protocol.ProtocolMessage) r2;
        r4 = TAG;
        r5 = new java.lang.StringBuilder;
        r5.<init>();
        r6 = "De-queueing message for new endpoint set: ";
        r5 = r5.append(r6);
        r6 = r2.getEndpointId();
        r6 = com.getpebble.android.framework.protocol.EndpointId.fromCode(r6);
        r5 = r5.append(r6);
        r6 = " (";
        r5 = r5.append(r6);
        r6 = r2.getEndpointId();
        r5 = r5.append(r6);
        r6 = ")";
        r5 = r5.append(r6);
        r5 = r5.toString();
        com.getpebble.android.common.core.trace.Trace.debug(r4, r5);
        r0 = r9.handleMessage(r2);
        if (r0 != 0) goto L_0x0083;
    L_0x00c9:
        r4 = TAG;
        r5 = new java.lang.StringBuilder;
        r5.<init>();
        r6 = "Re-queueing message: ";
        r5 = r5.append(r6);
        r6 = r2.getEndpointId();
        r6 = com.getpebble.android.framework.protocol.EndpointId.fromCode(r6);
        r5 = r5.append(r6);
        r6 = " (";
        r5 = r5.append(r6);
        r6 = r2.getEndpointId();
        r5 = r5.append(r6);
        r6 = ")";
        r5 = r5.append(r6);
        r5 = r5.toString();
        com.getpebble.android.common.core.trace.Trace.debug(r4, r5);
        r4 = r8.mQueueEndpoint;
        r4.onReceive(r2);
        goto L_0x0083;
    L_0x0103:
        r3 = move-exception;
        monitor-exit(r5);	 Catch:{ all -> 0x0103 }
        throw r3;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.getpebble.android.framework.comm.MessageRouter.setCurrentEndpointSet(com.getpebble.android.framework.endpoint.EndpointSet):boolean");
    }

    public boolean setEndpointSetToDefault(Context context) {
        return setCurrentEndpointSet(new NormalEndpointSet(context, this));
    }

    public boolean setEndpointSetToPrf(Context context) {
        return setCurrentEndpointSet(new PrfEndpointSet(context, this));
    }

    public boolean onRequest(EndpointRequest endpointRequest, FrameworkState frameworkState) {
        if (endpointRequest == null) {
            Trace.error(TAG, "Cannot send null request");
            return false;
        }
        EndpointSet currentEndpointSet = getCurrentEndpointSetLocked();
        if (currentEndpointSet == null || !(currentEndpointSet instanceof NormalEndpointSet)) {
            String endpointSet = "<none>";
            if (currentEndpointSet != null) {
                endpointSet = currentEndpointSet.getClass().getSimpleName();
            }
            Trace.debug(TAG, "Cannot handle this request (" + endpointRequest.getAction() + " / " + endpointRequest.getEndpointId() + ") right now, endpointSet: " + endpointSet);
            return false;
        }
        Trace.debug(TAG, "Sending request to normalEndpointSet for: " + endpointRequest.getEndpointId());
        return ((NormalEndpointSet) currentEndpointSet).handleRequest(endpointRequest, frameworkState);
    }

    private static boolean shouldLogIncomingMessage(EndpointId endpoint) {
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$protocol$EndpointId[endpoint.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
            case SMTPMessage.RETURN_HDRS /*2*/:
                return false;
            default:
                return true;
        }
    }

    public void onDeviceMessageReceived(PblDevice device, ProtocolMessage message) {
        if (this.mDevice.equals(device)) {
            boolean handled = false;
            EndpointSet currentEndpointSet = getCurrentEndpointSetLocked();
            if (currentEndpointSet != null) {
                if (shouldLogIncomingMessage(EndpointId.fromCode(message.getEndpointId()))) {
                    Trace.debug(TAG, String.format("Received protocol message from %s; sending to %s endpoint in %s (%d)", new Object[]{device.getName(), endpoint, currentEndpointSet.getClass().getSimpleName(), Short.valueOf(message.getEndpointId())}));
                }
                handled = currentEndpointSet.handleMessage(message);
            }
            if (!handled) {
                if (this.mQueueEndpoint.isSupported(EndpointId.fromCode(message.getEndpointId()))) {
                    Trace.debug(TAG, "Inbound message not handled; queueing: " + EndpointId.fromCode(message.getEndpointId()) + " (" + message.getEndpointId() + ")");
                    this.mQueueEndpoint.onReceive(message);
                    return;
                }
                Trace.debug(TAG, "Inbound message not handled; discarding: " + EndpointId.fromCode(message.getEndpointId()) + " (" + message.getEndpointId() + ")");
            }
        }
    }

    public void onMessageSendSuccess(PblDevice device) {
        if (this.mDevice.equals(device)) {
            EndpointSet currentEndpointSet = getCurrentEndpointSetLocked();
            if (currentEndpointSet != null) {
                currentEndpointSet.onMessageSendSuccess();
            }
        }
    }

    public void onMessageSendFailed(PblDevice device) {
        if (this.mDevice.equals(device)) {
            EndpointSet currentEndpointSet = getCurrentEndpointSetLocked();
            if (currentEndpointSet != null) {
                currentEndpointSet.onMessageSendFailed();
            }
        }
    }

    public static Set<MessageRouter> getAllRouters() {
        Set copyOf;
        synchronized ("ROUTER_LOCK") {
            copyOf = ImmutableSet.copyOf(sRouterMap.values());
        }
        return copyOf;
    }

    public boolean isInPrfEndpointSet() {
        return this.mCurrentEndpointSet instanceof PrfEndpointSet;
    }
}
